Function Initialize{
	[void][System.Reflection.Assembly]::LoadWithPartialName("MySql.Data")

	$connection = New-Object MySql.Data.MySqlClient.MySqlConnection
	$connectionString = "server=localhost;uid=root;pwd=;database=ancestra_static;"
	$connection.ConnectionString = $connectionString
	$connection.Open()
	
	$connection
}

Function Request($sql, $connection){
	$command = New-Object MySql.Data.MySqlClient.MySqlCommand($sql, $connection)
	$dataAdapter = New-Object MySql.Data.MySqlClient.MySqlDataAdapter($command)
	$dataSet = New-Object System.Data.DataSet
	$recordCount = $dataAdapter.Fill($dataSet, "data")
	
	$dataset
}

Function Close($connection){
	$connection.Close()
}

Function ConstructSortLvlUpdate($lvl, $invocID, $invocLvl){
	$posPart1 = $lvl1.IndexOf(";")
	$part1 = $lvl1.substring(0, $posPart1 + 1)
	
	$part2 = $lvl1.substring($posPart1 + 1)
	
	$posPart2 = $part2.IndexOf(";")
	$part2 = $part2.substring($posPart2 + 1)
	
	$posPart2 = $part2.IndexOf(";")
	$part2 = $part2.substring($posPart2)
	
	$lvl = "$part1$invocID;$invocLvl$part2"
	
	$lvl
}

Function MultiInvoc($sort, $connection, $lvl){
	$posPart1 = $sort.IndexOf(";")
	$part1 = $sort.substring(0, $posPart1 + 1)
	
	$part2 = $sort.substring($posPart1 + 1)
	
	$posPart2 = $part2.IndexOf(";")
	$invocID = $part2.substring(0, $posPart2)
	$part2 = $part2.substring($posPart2 + 1)
	
	$posPart2 = $part2.IndexOf(";")
	$part2 = $part2.substring($posPart2)
	
	$sql = "SELECT * FROM monsters WHERE id=$invocID"
	
	$monsterTable = Request $sql $connection
	
	$grades = $monsterTable.Tables["data"].Rows[0][4]
	
	$cptMonster = 0
	
	While($cptMonster -lt 5){
		$pos1 = $grades.IndexOf("@")
		$lvlMonsters += $grades.substring(0, $pos1)
		$pos2 = $grades.IndexOf("|")
		$grades = $grades.substring($pos2 + 1)
		
		$cptMonster ++
	}

	$lvlMonster = $lvlMonsters[$lvl]
	
	$lvl1 = "$part1$invocID;$lvlMonster$part2"
	
	$lvl1
}

$connection = Initialize

$sql = 'SELECT * FROM sorts WHERE lvl1 LIKE "181;%"'
$sortTable = Request $sql $connection

$nbRows = $sortTable.Tables["data"].Rows.Count
$cpt = 0

While($cpt -lt $nbRows){
	$lvlMonsters = @()
	
	$nom = $sortTable.Tables["data"].Rows[$cpt][1]
	
	If($nom.substring(0,1) -ne "["){
		
		$lvl1 = $sortTable.Tables["data"].Rows[$cpt][4]
		
		If($lvl1.contains("|") -ne $true){
			$posPart1 = $lvl1.IndexOf(";")
			$part1 = $lvl1.substring(0, $posPart1 + 1)
			
			$part2 = $lvl1.substring($posPart1 + 1)
			
			$posPart2 = $part2.IndexOf(";")
			$invocID = $part2.substring(0, $posPart2)
			$part2 = $part2.substring($posPart2 + 1)
			
			$posPart2 = $part2.IndexOf(";")
			$invocLvl = $part2.substring(0, $posPart2)
			$part2 = $part2.substring($posPart2)
			
			$sql = "SELECT * FROM monsters WHERE id=$invocID"

			$monsterTable = Request $sql $connection
			
			$grades = $monsterTable.Tables["data"].Rows[0][4]
			
			$cptMonster = 0
			
			While($cptMonster -lt 5){
				$pos1 = $grades.IndexOf("@")
				$lvlMonsters += $grades.substring(0, $pos1)
				$pos2 = $grades.IndexOf("|")
				$grades = $grades.substring($pos2 + 1)
				
				$cptMonster ++
			}
			
			$lvlMonster = $lvlMonsters[0]
			$lvl1 = "$part1$invocID;$lvlMonster$part2"
			
			$lvl = $sortTable.Tables["data"].Rows[$cpt][5]
			$lvl2 = ConstructSortLvlUpdate $lvl $invocID $lvlMonsters[1]
			
			$lvl = $sortTable.Tables["data"].Rows[$cpt][6]
			$lvl3 = ConstructSortLvlUpdate $lvl $invocID $lvlMonsters[2]
			
			$lvl = $sortTable.Tables["data"].Rows[$cpt][7]
			$lvl4 = ConstructSortLvlUpdate $lvl $invocID $lvlMonsters[3]
			
			$lvl = $sortTable.Tables["data"].Rows[$cpt][8]
			$lvl5 = ConstructSortLvlUpdate $lvl $invocID $lvlMonsters[4]
			
			$ID = $sortTable.Tables["data"].Rows[$cpt][0]
			$sql = "UPDATE ``sorts`` SET ``lvl1``='$lvl1',``lvl2``='$lvl2', ``lvl3``='$lvl3',``lvl4``='$lvl4', ``lvl5``='$lvl5' WHERE (``id``=$ID)"
			
			#$sql | out-file "D:\Projects\patchInvoc.sql" -append
			
			#$return = Request $sql $connection
		}
		Else{
			write-host $nom
			
			$lvlCorrect = ""
			
			$sort1Pos = $lvl1.IndexOf("|")
			$sort1 = $lvl1.substring(0,$sort1Pos)
			
			$lvlCorrect += MultiInvoc $sort1 $connection 0
			$lvlCorrect += "|"
			
			$lvl1 = $lvl1.substring($sort1Pos)
			
			While($lvl1.contains("|")){
				
				$lvl1 = $lvl1.substring(1, $lvl1.length - 1)
				
				If($lvl1.contains("|")){
					$sort1Pos = $lvl1.IndexOf("|")
					$sort1 = $lvl1.substring(0,$sort1Pos)
					
					$lvlCorrect += MultiInvoc $sort1 $connection 0
					$lvlCorrect += "|"
					
					$lvl1 = $lvl1.substring($sort1Pos)
				}
				Else{
					$lvlCorrect += MultiInvoc $lvl1 $connection 0
				}
			}
			
			$lvl1Correct = $lvlCorrect
			
			$lvl1 = $sortTable.Tables["data"].Rows[$cpt][5]
			
			$lvlCorrect = ""
			
			$sort1Pos = $lvl1.IndexOf("|")
			$sort1 = $lvl1.substring(0,$sort1Pos)
			
			$lvlCorrect += MultiInvoc $sort1 $connection 0
			$lvlCorrect += "|"
			
			$lvl1 = $lvl1.substring($sort1Pos)
			
			While($lvl1.contains("|")){
				
				$lvl1 = $lvl1.substring(1, $lvl1.length - 1)
				
				If($lvl1.contains("|")){
					$sort1Pos = $lvl1.IndexOf("|")
					$sort1 = $lvl1.substring(0,$sort1Pos)
					
					$lvlCorrect += MultiInvoc $sort1 $connection 0
					$lvlCorrect += "|"
					
					$lvl1 = $lvl1.substring($sort1Pos)
				}
				Else{
					$lvlCorrect += MultiInvoc $lvl1 $connection 0
				}
			}
			
			$lvl2Correct = $lvlCorrect
			
			$lvl1 = $sortTable.Tables["data"].Rows[$cpt][6]
			
			$lvlCorrect = ""
			
			$sort1Pos = $lvl1.IndexOf("|")
			$sort1 = $lvl1.substring(0,$sort1Pos)
			
			$lvlCorrect += MultiInvoc $sort1 $connection 0
			$lvlCorrect += "|"
			
			$lvl1 = $lvl1.substring($sort1Pos)
			
			While($lvl1.contains("|")){
				
				$lvl1 = $lvl1.substring(1, $lvl1.length - 1)
				
				If($lvl1.contains("|")){
					$sort1Pos = $lvl1.IndexOf("|")
					$sort1 = $lvl1.substring(0,$sort1Pos)
					
					$lvlCorrect += MultiInvoc $sort1 $connection 0
					$lvlCorrect += "|"
					
					$lvl1 = $lvl1.substring($sort1Pos)
				}
				Else{
					$lvlCorrect += MultiInvoc $lvl1 $connection 0
				}
			}
			
			$lvl3Correct = $lvlCorrect
			
			$lvl1 = $sortTable.Tables["data"].Rows[$cpt][7]
			
			$lvlCorrect = ""
			
			$sort1Pos = $lvl1.IndexOf("|")
			$sort1 = $lvl1.substring(0,$sort1Pos)
			
			$lvlCorrect += MultiInvoc $sort1 $connection 0
			$lvlCorrect += "|"
			
			$lvl1 = $lvl1.substring($sort1Pos)
			
			While($lvl1.contains("|")){
				
				$lvl1 = $lvl1.substring(1, $lvl1.length - 1)
				
				If($lvl1.contains("|")){
					$sort1Pos = $lvl1.IndexOf("|")
					$sort1 = $lvl1.substring(0,$sort1Pos)
					
					$lvlCorrect += MultiInvoc $sort1 $connection 0
					$lvlCorrect += "|"
					
					$lvl1 = $lvl1.substring($sort1Pos)
				}
				Else{
					$lvlCorrect += MultiInvoc $lvl1 $connection 0
				}
			}
			
			$lvl4Correct = $lvlCorrect
			
			$lvl1 = $sortTable.Tables["data"].Rows[$cpt][8]
			
			$lvlCorrect = ""
			
			$sort1Pos = $lvl1.IndexOf("|")
			$sort1 = $lvl1.substring(0,$sort1Pos)
			
			$lvlCorrect += MultiInvoc $sort1 $connection 0
			$lvlCorrect += "|"
			
			$lvl1 = $lvl1.substring($sort1Pos)
			
			While($lvl1.contains("|")){
				
				$lvl1 = $lvl1.substring(1, $lvl1.length - 1)
				
				If($lvl1.contains("|")){
					$sort1Pos = $lvl1.IndexOf("|")
					$sort1 = $lvl1.substring(0,$sort1Pos)
					
					$lvlCorrect += MultiInvoc $sort1 $connection 0
					$lvlCorrect += "|"
					
					$lvl1 = $lvl1.substring($sort1Pos)
				}
				Else{
					$lvlCorrect += MultiInvoc $lvl1 $connection 0
				}
			}
			
			$lvl5Correct = $lvlCorrect
			
			$ID = $sortTable.Tables["data"].Rows[$cpt][0]
			$sql = "UPDATE ``sorts`` SET ``lvl1``='$lvl1Correct',``lvl2``='$lvl2Correct', ``lvl3``='$lvl3Correct',``lvl4``='$lvl4Correct', ``lvl5``='$lvl5Correct' WHERE (``id``=$ID)"
			
			#$sql | out-file "D:\Projects\patchInvocMulti.sql" -append
			
			#$return = Request $sql $connection
		}
	}
	$cpt ++
}

#$return = Request $sql $connection

Close $connection